package com.igeek.dao;

import com.igeek.bean.Car;
import com.igeek.bean.User;
import com.igeek.client.MainSys;
import com.igeek.db.DataDB;
import com.igeek.util.SystemEnum;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class UserDaoImpl implements UserDao{

    //从数据源中获取用户集合
    List<User> users = DataDB.getInstance().users;
    CarDao carDao = new CarDao();

    Scanner scanner = new Scanner(System.in);
    @Override
    public int login(String username, String password) {

        boolean flag = false;
        for(User user : users){
            //匹配用户信息

            if(user.getUsername().equals(username) && user.getPassword().equals(password)){

                //如果匹配到了用户，那么就记录一下当前登录用户的信息，以便之后的操作可以找到当前登录人
                DataDB.getInstance().currentUser = user;
                flag = true;
            }
        }


        return flag?SystemEnum.SUCCESS.getCode() : SystemEnum.ERROR_INPUT.getCode(); //正确0，错误1
    }

    public int checkname(String name){
        for(User user : users){
            if(user.getUsername().equals(name)){
                return SystemEnum.ERROR_REPEAT.getCode();
            }
        }
        return SystemEnum.SUCCESS.getCode();

    }

    public void showcompareinfo(){
        MainSys mainSys = new MainSys();
        if(DataDB.getInstance().currentUser == null){
            System.out.println("当前无登录用户");

            mainSys.login();
        }else{
            List<Car> collectcar = DataDB.getInstance().currentUser.getComparelist();
            if(collectcar.size() == 0){
                System.out.println("暂时无对比车辆");
                MainSys.cai(mainSys);
            }else{
                for (int i = 0;i < collectcar.size();i ++) {
                    System.out.println(collectcar.get(i));
                }
                MainSys.cai(mainSys);
            }

        }



    }

    public void deletecompare(int index){
        List<Car> collectcar = DataDB.getInstance().currentUser.getCars();
        collectcar.remove(index);

    }


}
